package com.yanqu.road.dao.impl.activity.gashapon;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.gashapon.data.GashaponUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : Cxp
 * @Time : 2024-10-21
 * @Description : 幸运扭蛋-用户 dao
 */

public class GashaponUserDataDaoImpl extends TempDataObjectDao<GashaponUserData> {
    public static String getGashaponUserDataSql = "select * from `t_u_gashapon_data` where `activityId`=? and " +
            "`userId`=?";

    public GashaponUserData getGashaponUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getGashaponUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateGashaponUserDataSql = "update `t_u_gashapon_data` set score=?,guaranteeCnt=?," +
            "excellentTarget=?,excellentCnt=?,drawCnt=?,isLock=?,isAnima=?,choose=?,optional=?,selected=? where " +
            "activityId=? and userId=?";

    @Override
    protected boolean updateData(GashaponUserData gashaponUserData) {
        gashaponUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, gashaponUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, gashaponUserData.getGuaranteeCnt()));
        para.put(i++, new DbParameter(Types.INTEGER, gashaponUserData.getExcellentTarget()));
        para.put(i++, new DbParameter(Types.INTEGER, gashaponUserData.getExcellentCnt()));
        para.put(i++, new DbParameter(Types.INTEGER, gashaponUserData.getDrawCnt()));
        para.put(i++, new DbParameter(Types.BOOLEAN, gashaponUserData.isLock()));
        para.put(i++, new DbParameter(Types.BOOLEAN, gashaponUserData.isAnima()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(gashaponUserData.getChoose())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(gashaponUserData.getOptional())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(gashaponUserData.getSelected())));
        para.put(i++, new DbParameter(Types.INTEGER, gashaponUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, gashaponUserData.getUserId()));
        if (getDb().executeNoneQuery(updateGashaponUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGashaponUserDataSql = "insert into `t_u_gashapon_data`(activityId,userId,score," +
            "guaranteeCnt,excellentTarget,excellentCnt,drawCnt,isLock,isAnima,choose,optional,selected) values (?,?," + "?,?,?,?,?,?,?," + "?,?,?)";

    @Override
    protected boolean addData(GashaponUserData gashaponUserData) {
        gashaponUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, gashaponUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, gashaponUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, gashaponUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, gashaponUserData.getGuaranteeCnt()));
        para.put(i++, new DbParameter(Types.INTEGER, gashaponUserData.getExcellentTarget()));
        para.put(i++, new DbParameter(Types.INTEGER, gashaponUserData.getExcellentCnt()));
        para.put(i++, new DbParameter(Types.INTEGER, gashaponUserData.getDrawCnt()));
        para.put(i++, new DbParameter(Types.BOOLEAN, gashaponUserData.isLock()));
        para.put(i++, new DbParameter(Types.BOOLEAN, gashaponUserData.isAnima()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(gashaponUserData.getChoose())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(gashaponUserData.getOptional())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(gashaponUserData.getSelected())));
        if (getDb().executeNoneQuery(insertGashaponUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected GashaponUserData parseData(ResultSet rs) throws SQLException {
        GashaponUserData gashaponUserData = new GashaponUserData();
        gashaponUserData.setActivityId(rs.getInt("activityId"));
        gashaponUserData.setUserId(rs.getLong("userId"));
        gashaponUserData.setScore(rs.getLong("score"));
        gashaponUserData.setGuaranteeCnt(rs.getInt("guaranteeCnt"));
        gashaponUserData.setExcellentTarget(rs.getInt("excellentTarget"));
        gashaponUserData.setExcellentCnt(rs.getInt("excellentCnt"));
        gashaponUserData.setDrawCnt(rs.getInt("drawCnt"));
        gashaponUserData.setLock(rs.getBoolean("isLock"));
        gashaponUserData.setAnima(rs.getBoolean("isAnima"));
        gashaponUserData.setChoose(JSON.parseObject(rs.getString("choose"),
                new TypeReference<java.util.Map<java.lang.Integer, java.util.Map<java.lang.Integer,
                        java.lang.Integer>>>() {
        }));
        gashaponUserData.setOptional(JSON.parseObject(rs.getString("optional"),
                new TypeReference<java.util.Map<java.lang.Integer, java.util.Map<java.lang.Integer,
                        java.lang.Integer>>>() {
        }));
        gashaponUserData.setSelected(JSON.parseObject(rs.getString("selected"),
                new TypeReference<java.util.Map<java.lang.Integer, java.util.Map<java.lang.Integer,
                        java.lang.Integer>>>() {
        }));

        return gashaponUserData;
    }
}
